package com.lds.erp.order.cart;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dph.common.utils.base.BaseController;
import com.dph.common.utils.base.Quantity;
import com.dph.common.utils.utils.AppUtils;
import com.github.pagehelper.util.StringUtil;
import com.lds.erp.base.JsonResult;
import com.lds.erp.common.AppEnum;
import com.lds.erp.common.util.AccountCache;
import com.lds.erp.common.util.AccountUtils;
import com.lds.erp.order.OrderCartClient;
import com.lds.erp.order.cart.ao.*;
import com.lds.erp.order.cart.order.*;
import com.lds.erp.order.cart.result.*;
import com.lds.erp.order.cart.vo.*;
import com.lds.erp.util.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.lds.erp.gateway.interceptor.SignValidInterceptor.TOKEN_PARAM_KEY;

/**
 * @author liuli
 */
@RestController
@Api(value = "购物车操作", description = "购物车操作", tags = {"购物车操作接口"})
@RequestMapping(value = "/api/order/cart/")
public class CartController extends BaseController {
    @Autowired
    private OrderCartClient orderCartClient;

    @ApiOperation(value = "购物车修改(修改以后购物车商品数量)", notes = "购物车修改")
    @PostMapping("update_cart")
    public JsonResult<OrderCartUpAndDownVO> updateCart(@RequestBody OrderCartUpAndDownAO ao, HttpServletRequest request) {


        //AccountCache accountCache = AccountUtils.getAccount();
        AccountCache accountCache = null;
        String token = request.getHeader(TOKEN_PARAM_KEY);

        if (token != null)
        {
            accountCache = AccountUtils.getAccount(token);
        }

        if (accountCache == null)
        {
            accountCache = AccountUtils.getAccount();
        }

        OrderCartUpAndDownOrder order = new OrderCartUpAndDownOrder();
        BeanUtils.copyProperties(ao, order);
        order.setSerialNo(AppUtils.newNo());
        order.setCartMchId(accountCache.getMchId());
        order.setCartMchName(accountCache.getMchName());
        order.setCartOrderSource(AppEnum.MCHMP);
        order.setDistrictId(ao.getDistrictId());
        OrderCartUpAndDownResult result = orderCartClient.orderCartUpAndDown(order);
        return result.isSuccess() ? JsonUtils.success(result, OrderCartUpAndDownVO.class) : JsonUtils.fail(result.getMessage());
    }

    @ApiOperation(value = "删除购物车商品", notes = "删除购物车商品")
    @PostMapping("delete_cart")
    public JsonResult<OrderCartDeleteVO> deleteCart(@RequestBody OrderCartDeleteAO ao) {

        OrderCartDeleteOrder order = new OrderCartDeleteOrder();
        BeanUtils.copyProperties(ao, order);
        order.setSerialNo(AppUtils.newNo());
        AccountCache accountCache = AccountUtils.getAccount();
        order.setCartMchId(accountCache.getMchId());
        order.setCartOrderSource(AppEnum.MCHMP);
        OrderCartDeleteResult result = orderCartClient.orderCartDelete(order);
        return result.isSuccess() ? JsonUtils.success(result, OrderCartDeleteVO.class) : JsonUtils.fail(result.getMessage());
    }

    @ApiOperation(value = "获取购物车详情", notes = "获取购物车详情")
    @GetMapping("cart_detail")
    public JsonResult<OrderCartDetailVO> cartDetail(OrderCartDetailAO ao, HttpServletRequest request) {

        //AccountCache accountCache = AccountUtils.getAccount();
        Integer districtId = null;
        if (StringUtil.isNotEmpty(request.getParameter("districtId"))) {
            districtId = Integer.parseInt(request.getParameter("districtId"));
        }
        AccountCache accountCache = null;
        String token = request.getHeader(TOKEN_PARAM_KEY);

        if (token != null)
        {
            accountCache = AccountUtils.getAccount(token);
        }

        if (accountCache == null)
        {
            accountCache = AccountUtils.getAccount();
        }


        OrderCartDetailOrder order = new OrderCartDetailOrder();

        order.setSerialNo(AppUtils.newNo());
        order.setCartMchId(accountCache.getMchId());
        order.setDistrictId(districtId);
        order.setSsuCode(ao.getSsuCode());

        OrderCartDetailResult result = orderCartClient.orderCartDetail(order);
        return result.isSuccess() ? JsonUtils.success(result, OrderCartDetailVO.class) : JsonUtils.fail(result.getMessage());
    }

    @ApiOperation(value = "单个商品购物车数量", notes = "单个商品购物车数量")
    @GetMapping("product_num")
    public JsonResult<OrderCartProductDetailVO> productNum(OrderCartProductDetailAO ao) {
        OrderCartProductDetailOrder order = new OrderCartProductDetailOrder();
        order.setSsuCode(ao.getSsuCode());
        AccountCache accountCache = AccountUtils.getAccount();
        order.setCartMchId(accountCache.getMchId());
        order.setSerialNo(AppUtils.newNo());
        OrderCartProductDetailResult result = orderCartClient.orderCartProductDetailBySsuCode(order);
        return result.isSuccess() ? JsonUtils.success(result, OrderCartProductDetailVO.class) : JsonUtils.fail(result.getMessage());
    }

    @ApiOperation(value = "获取购物车数量", notes = "获取购物车数量")
    @GetMapping("cart_num")
    public JsonResult<OrderCartNumVO> cartNum(OrderCartNumAO ao, HttpServletRequest request) {

        //AccountCache accountCache = AccountUtils.getAccount();
        AccountCache accountCache = null;
        String token = request.getHeader(TOKEN_PARAM_KEY);

        if (token != null)
        {
            accountCache = AccountUtils.getAccount(token);
        }

        if (accountCache == null)
        {
            accountCache = AccountUtils.getAccount();
        }

        OrderCartNumOrder order = new OrderCartNumOrder();

        if(null==accountCache){
        	OrderCartNumResult result = new OrderCartNumResult();
        	result.setTotalNum(Quantity.zero());
        	return JsonUtils.success(result, OrderCartNumVO.class);

        }
        order.setSerialNo(AppUtils.newNo());
        order.setCartMchId(accountCache.getMchId());
        OrderCartNumResult result = orderCartClient.orderCartNum(order);
        return result.isSuccess() ? JsonUtils.success(result, OrderCartNumVO.class) : JsonUtils.fail(result.getMessage());
    }

    @ApiOperation(value = "购物车去结算", notes = "购物车去结算")
    @PostMapping("settlement_cart")
    public JsonResult<QueryOrderCartVO> settlementCart(@RequestBody QueryOrderCartAO ao) {
        String queryOrderCartOrderStr = JSONObject.toJSONString(ao);
        QueryOrderCartOrder order = JSON.parseObject(queryOrderCartOrderStr, QueryOrderCartOrder.class);
        order.setSerialNo(AppUtils.newNo());
        if (ao.getAgent() != null  &&  ao.getAgent() == 1)
        {
            if (ao.getMchId() != null)
            {
                order.setCartMchId(ao.getMchId());
            }
            else
            {
                JsonUtils.fail("代理下单客户id不能为空");
            }
        }
        else
        {
            AccountCache accountCache = AccountUtils.getAccount();
            order.setCartMchId(accountCache.getMchId());
        }

        QueryOrderCartResult result = orderCartClient.queryOrderCart(order);
        return result.isSuccess() ? JsonUtils.success(result, QueryOrderCartVO.class) : JsonUtils.fail(result.getMessage());
    }

    @ApiOperation(value = "收银台请求参数", notes = "收银台请求参数")
    @PostMapping("order_cashier")
    public JsonResult<QueryOrderCashierVO> order_cashier(@RequestBody QueryOrderCashierAO ao) {
//        String queryOrderCashierOrderStr = JSONObject.toJSONString(ao);
//        QueryOrderCashierOrder order = JSON.parseObject(queryOrderCashierOrderStr, QueryOrderCashierOrder.class);
        QueryOrderCashierOrder order = new QueryOrderCashierOrder();
        order.setSerialNo(AppUtils.newNo());

//        if (ao.getAgent() != null  &&  ao.getAgent() == 1)
//        {
//            if (ao.getMchId() != null)
//            {
//                order.setCartMchId(ao.getMchId());
//            }
//            else
//            {
//                JsonUtils.fail("代理下单客户id不能为空");
//            }
//        }
//        else
//        {
//            AccountCache accountCache = AccountUtils.getAccount();
//            order.setCartMchId(accountCache.getMchId());
//        }

        AccountCache accountCache = AccountUtils.getAccount();
        order.setCartMchId(accountCache.getMchId());
        order.setImmediatelyList(ao.getImmediatelyList());
        order.setCurrentUserId(ao.getMchId());
        QueryOrderCashierResult result = orderCartClient.queryOrderCashier(order);
        return result.isSuccess() ? JsonUtils.success(result, QueryOrderCashierVO.class) : JsonUtils.fail(result.getMessage());
    }

    @ApiOperation(value = "再次支付收银台", notes = "再次支付收银台请求参数")
    @PostMapping("order_pay_again")
    public JsonResult<OrderPayAgainVO> orderPayAgain(@RequestBody OrderPayAgainAO ao) {

        QueryPayAgainCashierOrder order =new QueryPayAgainCashierOrder();
        order.setSerialNo(AppUtils.newNo());
        order.setOrderNo(ao.getOrdOrderNo());
        QueryPayAgainCashierResult result = orderCartClient.queryPayAgainCashier(order);
        return result.isSuccess() ? JsonUtils.success(result, OrderPayAgainVO.class) : JsonUtils.fail(result.getMessage());
    }

}
